/**
 * 
 */
package com.mms.service.impl;

import com.mms.context.Constants;
import com.mms.entity.model.JsonData;
import com.mms.entity.model.PagerTool;
import com.mms.entity.model.TreeNode;
import com.mms.entity.service.impl.EntityServiceImpl;
import com.mms.pojo.SysMenu;
import com.mms.service.SysMenuService;
import com.mms.utils.RequestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * Created by dangz on 2017/5/21/021.
 */
@Service
public class SysMenuServiceImpl extends EntityServiceImpl<SysMenu> implements SysMenuService {
	
	@Override
	public List<SysMenu> listRoot() {
		String hql = "from SysMenu a where a.sysMenu is null order by a.ordno";
		return find(hql);
	}
	
	@Override
	public List<SysMenu> getAll() {
		String hql = "from SysMenu a order by a.ordno";
		return find(hql);
	}

	private List<TreeNode> createTree(List<SysMenu> l, Long accountid) {
		List<TreeNode> tree = new ArrayList<TreeNode>();
		String hql = "from SysMenu a where a.sysMenu.id=? and a.status=1 order by a.ordno";
		List<SysMenu> list = null;
		for (SysMenu a : l) {
			TreeNode t = new TreeNode();
			t.setId(a.getId().toString());
			t.setText(a.getName());
			if(accountid == null) {
				list = find(hql, a.getId());
			} else {
				list = getChildMenuInPermission(a.getId(), accountid);
			}
			if (!list.isEmpty()) {
				t.setChildren(createTree(list, accountid));
			}
			tree.add(t);
		}
		return tree;
	}
	
	@Override
	public List<Map<String, Object>> getMenuExclude(Long menuid) {
		StringBuffer hql = new StringBuffer();
		hql.append("select new map(a.id as id,a.sysMenu.id as pid,a.name as name,a.nlevel as nlevel) from SysMenu a where 1=1");
		if(menuid != null) {
			hql.append(" and a.scort not like ?");
		}
		hql.append(" order by a.scort,a.ordno");
		if(menuid != null) {
			return find(hql.toString(), "%,"+menuid+",%");
		}
		return find(hql.toString());
	}
	
	@Override
	public List<SysMenu> getChildMenuInPermission(Long menuid, Long accountid) {
		String hql = "select a from SysMenu a join a.sysOperates b join b.sysRoles c join c.sysManagers d where d.id=? and b.id.op='list' and a.status=1";
		if(menuid == null) {
			hql += " and a.sysMenu is null order by a.ordno";
			return find(hql, accountid);
		} else {
			hql += " and a.sysMenu.id=? order by a.ordno";
			return find(hql, accountid, menuid);
		}
	}
	
	@Override
	public List<SysMenu> getChildMenuInPermissionByRole(Long menuid, String role) {
		String hql = "select a from SysMenu a join a.sysOperates b join b.sysRoles c where c.code = ? and b.id.op = 'list' and a.status = 1";
		if(menuid == null) {
			hql += " and a.sysMenu is null order by a.ordno";
			return find(hql, role);
		} else {
			hql += " and a.sysMenu.id=? order by a.ordno";
			return find(hql, role, menuid);
		}
	}

	@Override
	public void setOrder(Long id, Long order) {
		String hql = "UPDATE SysMenu t set t.ordno=? where t.id=?";
		bulkUpdate(hql, order, id);
	}
	
	@Override
	public void resetOrder() {
		String hql = "UPDATE SysMenu t set t.ordno=?";
		bulkUpdate(hql, Constants.ORDERNO);
	}


	@Override
	public JsonData getList(PagerTool pt, HttpServletRequest req) {
		StringBuffer cb = new StringBuffer("from SysMenu a where 1=1");
		StringBuffer lb = new StringBuffer("select new map(a.id as id,a.name as name,a.param as param,a.ordno as ordno,a.img as img,date_format(a.addTime,'%Y-%m-%d') as addtime,date_format(a.updateTime,'%Y-%m-%d') as updateTime,a.status as status) from SysMenu a where 1=1 ");
		Map<String, Object> params = RequestUtils.getQueryParams(req);;
		if (params.get("name") != null && params.get("name") != "" ){
			cb.append("  and a.name like '%"+params.get("name")+"%'");
			lb.append("  and a.name like '%"+params.get("name")+"%'");
		}if (params.get("pdate") != null && params.get("pdate") != "" ){
			cb.append("  and a.addTime like '%"+params.get("pdate")+"%'");
			lb.append("  and a.addTime like '%"+params.get("pdate")+"%'");
		}
		if (StringUtils.isNotEmpty(pt.getSortby())){
			lb.append("  order by "+pt.getSortby()+" "+pt.getSorttype());
		}
		JsonData jd = new JsonData();
		jd.setTotal(getCount(cb.toString()));
		jd.setRoot(find(pt.getStart(),pt.getEnd(),lb.toString()));
		return jd;
	}
}